package com.game.utils;

import com.game.GameMain;
import com.game.data.BagData;
import com.game.data.MapData;
import com.game.data.RoleData;
import com.game.model.Bag;
import com.game.model.equip.Equip;
import com.game.model.api.EquipType;
import com.game.model.map.GameMap;
import com.game.model.role.NPC;
import com.game.model.role.PlayerRole;
import com.game.model.task.Task;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:文件保存
 **/
public class Storage {
    //将游戏数据放到map集合里
    private static Map<String, Object> getDataMap() {
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("roleData", RoleData.playerRole);
        dataMap.put("equipData", RoleData.equipMap);
        dataMap.put("currentMap", MapData.gameMap);
        dataMap.put("mapData", MapData.mapList);
        dataMap.put("bagData", BagData.bag);
        dataMap.put("taskMap",RoleData.taskMap);
        dataMap.put("npcMap",RoleData.npcMap);
        return dataMap;
    }

    //保存游戏到文件
    public static Boolean saveGame() {
        File file = new File(getProjectPath()+"\\storage\\gameData.dat");
        try {
            //如果没有存档目录文件夹
            if (!file.exists()) {
                //创建目录
                boolean mkdirs = file.getParentFile().mkdirs();
                //创建文件
                if (mkdirs) file.createNewFile();
            }
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(getDataMap());
            oos.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    //读取游戏进度数据
    public static boolean loadGame() {
        File file = new File(getProjectPath()+"/storage/gameData.dat");
        if (!file.exists()) {
            return false;
        }
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
            Map gameMap = (Map) ois.readObject();
            RoleData.playerRole = (PlayerRole) gameMap.get("roleData");
            RoleData.equipMap = (Map<EquipType, Equip>) gameMap.get("equipData");
            MapData.gameMap = (GameMap) gameMap.get("currentMap");
            MapData.mapList = (List<GameMap>) gameMap.get("mapData");
            BagData.bag = (Bag) gameMap.get("bagData");
            RoleData.taskMap = (Map<Integer, Task>)gameMap.get("taskMap");
            RoleData.npcMap = (Map<Integer, NPC>)gameMap.get("npcMap");
            ois.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }


    //获取打包的jar位置
    public static String getProjectPath() {
        java.net.URL url = GameMain.class.getProtectionDomain().getCodeSource().getLocation();
        String filePath = null;
        try {
            filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (filePath.endsWith(".jar")) filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
        File file = new File(filePath);
        filePath = file.getAbsolutePath();
        return filePath;
    }
}
